package swipe;

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.math.MathUtils;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;

import me.young.android.fullscreengesture.SettingFragment;

/* compiled from: SwipeGesture.kt */
public final class SwipeGesture {
    private final boolean quickSwipe;
    private final int swipeDistance;
    private float a;
    private float b = 0.5f;
    private float c;
    private ValueAnimator valueAnimator;
    private VelocityTracker e;
    private final float f;
    private final View view;
    private final OnGesture onGesture;
    private SwipeIndicator.OnSwipe onSwipe;
    private final boolean validate;
    private boolean handle = true;
    private boolean reachbackFlag;

    /* compiled from: Animator.kt */
    public static final class a implements AnimatorUpdateListener {
        final /* synthetic */ SwipeGesture swipeGesture;

        public a(SwipeGesture dVar) {
            this.swipeGesture = dVar;
        }

        public final void onAnimationUpdate(ValueAnimator valueAnimator) {
            Object animatedValue = valueAnimator.getAnimatedValue();
            if (animatedValue == null) {
                throw new ClassCastException("null cannot be cast to non-null type kotlin.Float");
            }
            swipeGesture.c = (float) animatedValue;
            swipeGesture.callOnGesture();
        }
    }

    public SwipeGesture(View view, OnGesture onGesture, SwipeIndicator.OnSwipe onSwipe, boolean onValidate) {
        this.view = view;
        this.onGesture = onGesture;
        this.onSwipe = onSwipe;
        this.validate = onValidate;
        SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(view.getContext());
        quickSwipe = defaultSharedPreferences.getBoolean(SettingFragment.KEY_PREF_QUICK_SWIPE, true);
        swipeDistance = defaultSharedPreferences.getInt(SettingFragment.KEY_PREF_SWIPE_DISTANCE, 35);
        ViewConfiguration viewConfiguration = ViewConfiguration.get(this.view.getContext());
        this.f = (float) viewConfiguration.getScaledMaximumFlingVelocity();
        view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(View v) {
            }

            @Override
            public void onViewDetachedFromWindow(View v) {
                SwipeGesture.this.clear();
            }
        });
    }

    public final View a() {
        return this.view;
    }

    private final boolean b() {
        ValueAnimator valueAnimator = this.valueAnimator;
        return valueAnimator != null && valueAnimator.isRunning();
    }

    private float c() {
        return ((float) this.view.getContext().getResources().getDisplayMetrics().widthPixels) * ((float) swipeDistance / 100f);
    }

    public final boolean a(MotionEvent motionEvent) {
        boolean z = false;
        VelocityTracker velocityTracker;
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (!this.validate || b()) {
                    handle = false;
                    return false;
                }
                if (this.e == null) {
                    this.e = VelocityTracker.obtain();
                } else {
                    velocityTracker = this.e;
                    velocityTracker.clear();
                }
                this.a = motionEvent.getX();
                this.b = motionEvent.getY() / ((float) a().getHeight());
                velocityTracker = this.e;
                if (velocityTracker == null) {
                    return true;
                }
                velocityTracker.addMovement(motionEvent);
                return true;
            case MotionEvent.ACTION_UP:
                if (!handle) {
                    handle = true;
                    return false;
                }
                if (Math.abs(motionEvent.getX() - this.a) >= c()) {
                    z = true;
                } else {
                    velocityTracker = this.e;
                    if (velocityTracker != null) {
                        velocityTracker.computeCurrentVelocity(1000, this.f);
                        boolean z2 = velocityTracker.getXVelocity() > ((float) 500);
                        z = z2 && quickSwipe;
                        if (z) {
                            if (onGesture != null && !reachbackFlag) {
                                onGesture.onReachBack();
                            }
                        }
                    }
                }
                velocityTracker = this.e;
                if (velocityTracker != null) {
                    velocityTracker.clear();
                    velocityTracker.recycle();
                }
                this.e = null;
                f();
                if (z) {
                    this.c = 0;
                    swipeFinish();
                }
                return true;
            case MotionEvent.ACTION_MOVE:
                if (!handle) {
                    return false;
                }
                this.c = MathUtils.clamp(Math.abs(motionEvent.getX() - this.a) / this.c(), 0, 1.0f);
                this.b = motionEvent.getY() / ((float) view.getHeight());
                if (this.c == 1.0f) {
                    if (onGesture != null && !reachbackFlag) {
                        onGesture.onReachBack();
                    }
                    reachbackFlag = true;
                } else {
                    reachbackFlag = false;
                }
                callOnGesture();
                velocityTracker = this.e;
                if (velocityTracker == null) {
                    return true;
                }
                velocityTracker.addMovement(motionEvent);
                return true;
            default:
                return true;
        }
    }

    private void clear() {
        ValueAnimator valueAnimator = this.valueAnimator;
        if (valueAnimator != null) {
            valueAnimator.removeAllUpdateListeners();
            valueAnimator.removeAllListeners();
            valueAnimator.cancel();
        }
    }

    private final void callOnGesture() {
        if (this.onGesture != null) {
            onGesture.onGesture(this.b, this.c);
        }
    }

    private void swipeFinish() {
        if (onSwipe != null) {
            onSwipe.onSwipe();
        }
    }

    private void f() {
        ValueAnimator ofFloat = ValueAnimator.ofFloat(this.c, 0);
        ofFloat.setDuration(150);
        ofFloat.setInterpolator(new DecelerateInterpolator());
        ofFloat.addUpdateListener(new a(this));
        this.valueAnimator = ofFloat;
        this.valueAnimator.start();
    }

    public interface OnGesture {
        void onGesture(float f1, float f2);

        void onReachBack();
    }

}
